<template>
  <el-row
    type="flex"
    justify="space-between"
    align="middle"
    style="height: 40px; width: 100%"
  >
    <el-col>{{ treeData.name }}</el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <el-col>{{ treeData.manager }}</el-col>
        <el-col style="cursor: pointer">
          <el-dropdown @command="handleCommand">
            <span class="el-dropdown-link">
              操作<i class="el-icon-arrow-down el-icon--right" />
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <el-dropdown-item
                v-if="!isRoot"
                command="edit"
              >编辑部门</el-dropdown-item>
              <el-dropdown-item
                v-if="!isRoot"
                command="del"
              >删除部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartments } from '@/api/departments'
export default {
  props: {
    treeData: {
      required: true, // 必须传入
      type: Object // 传入对象类型为对象
    },
    isRoot: {
      type: Boolean
    }
  },
  data() {
    return {
      titleName: ''
    }
  },
  methods: {
    handleCommand(type) {
      if (type === 'add') {
        // 点击添加部门
        this.$emit('addDepts', this.treeData)
        console.log(this.titleName)
        // this.$emit('titleNames', this.titleName)
      } else if (type === 'edit') {
        // 点击编辑部门
        this.$emit('editDepts', this.treeData)
      } else {
        // 删除子节点
        this.$confirm('确定要删除该部门吗', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            return delDepartments(this.treeData.id)
          })
          .then(() => {
            this.$message.success('删除成功')
            this.$emit('delTree')
          })
      }
      this.$emit('titleNames', this.titleName)
    }
  }
}
</script>

<style></style>
